﻿SSVC.Wrap.Dojo.SetObject("SSVC.SVG.ActiveLabelElement",
 SSVC.Wrap.Dojo.Declare("ActiveLabelElement", SSVC.SVG.ActiveElement,
{
	TextArea: null,
	TextElementHelper: null,
	TextAreaHeight: null,
	currX: null,
	currY: null,
	updateOptions: { all: 0, tail: 2, head: 1 },
	updateMode: 0,

	_textRows: 1,
	constructor: function(fontSize)
	{
		this.FontSize = fontSize;
	},

	OnMouseUp: function(callElement)
	{
		if (callElement.selectedPoint == null)
		{
			callElement.TextArea.focus();

		}
		callElement.inherited(arguments);
	},

	OnTextAreaKeyPress: function(callElement)
	{
		var newWidth = callElement.TextArea.offsetWidth + callElement.Radius * 2;
		callElement.OX = newWidth;

		callElement.Refresh();

		if (arguments[1].keyCode == 13 && !arguments[1].ctrlKey && !arguments[1].shiftKey)
		{
			callElement.TextArea.blur();
			callElement.SelectMode = SSVC.SVG.ActiveElement.Enums.SelectMode.None;
			callElement.Refresh();
		}
	},

	OnTextAreaMouseDown: function(callElement)
	{
		callElement.TextArea.focus();
	},

	OnTextAreaKeyUp: function(callElement)
	{
		var newWidth = callElement.TextArea.offsetWidth + callElement.Radius * 2;
		callElement.OX = newWidth;

		callElement.Refresh();
	},

	OnTextAreaFocus: function(callElement)
	{
		callElement.Select();

	},

	OnTopMouseDown: function(callElement)
	{
		callElement.updateMode = callElement.updateOptions.tail;
		callElement.selectedTop = arguments[1];
	},

	OnTopMouseUp: function(callElement)
	{
	},

	OnTailMouseDown: function(callElement)
	{
		callElement.updateMode = callElement.updateOptions.all;
	},

	OnHeadMouseDown: function(callElement)
	{
		callElement.updateMode = callElement.updateOptions.head;
	},

	Draw: function(startX, startY, currX, currY, shiftKey)
	{
		this.SelectMode = this.SelectOptions.None;

		this.startX = startX;
		this.startY = startY;
		this.Radius = this.FontSize;
		this.OY = this.OX = +this.FontSize * 2;
		this.X = +startX - this.OX / 2;
		this.Y = +startY - this.OY - this.OY / 2;
		this.TextArea.style.minWidth = "2px";
		this.TextArea.style.minHeight = this.OY + "px";
		this.Refresh(true);
	},

	Move: function(x, y, shiftKey)
	{
		this.SelectMode = this.SelectOptions.Selected;
		this._move(x, y, shiftKey);

		this.Refresh();
	},

	_move: function(x, y, shiftKey)
	{
		if (!this.selectedPoint) return;
		if (this.updateMode == this.updateOptions.tail)
		{
			this.startX = x;
			this.startY = y;
			return;
		}
		var deltaX = x - this.selectedPoint.x;
		var deltaY = y - this.selectedPoint.y;

		this.selectedPoint.x = x;
		this.selectedPoint.y = y;
		this.X += deltaX;
		this.Y += deltaY;
		if (this.updateMode == this.updateOptions.all)
		{
			this.startX += deltaX;
			this.startY += deltaY;
		}
	},

	_addInnerElement: function()
	{
		this.InnerElement = new SSVC.SVG.Group();

		this.InnerElement.LineWidth = 3;
		this.InnerElement.LineColor = "red";

		this.InnerRect = new SSVC.SVG.Rectangle();
		this.InnerRect.Background = "white";

		this.InnerTriangle = new SSVC.SVG.Triangle();
		this.InnerTriangle.Background = "red";

		SSVC.Wrap.Dojo.On(this.InnerTriangle.Node, "mousedown", SSVC.Wrap.Dojo.Partial(this.OnTailMouseDown, this));
		SSVC.Wrap.Dojo.On(this.InnerRect.Node, "mousedown", SSVC.Wrap.Dojo.Partial(this.OnHeadMouseDown, this));
		SSVC.Wrap.Dojo.On(this.InnerRect.Node, "dblclick", SSVC.Wrap.Dojo.Partial(this.OnTextAreaMouseDown, this));

		this.InnerElement.addChild(this.InnerTriangle);
		this.InnerElement.addChild(this.InnerRect);

		this.TextArea = document.createElement("span");
		this.TextArea.setAttribute("contentEditable", true);
		document.body.appendChild(this.TextArea);
		this.TextArea.className = "EditableTextArea";

		this.TextArea.id = "EditableTextArea_" + SSVC.SVG.Shape.MakeName(6);

		this.offset = !window.opera ? (this.TextArea.offsetHeight - this.TextArea.clientHeight) : (this.TextArea.offsetHeight + parseInt(window.getComputedStyle(this.TextArea, null).getPropertyValue("border-top-width")));

		this.TextArea.addEventListener("keypress", SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyPress, this));
		this.TextArea.addEventListener("keyup", SSVC.Wrap.Dojo.Partial(this.OnTextAreaKeyUp, this));
		this.TextArea.addEventListener("click", SSVC.Wrap.Dojo.Partial(this.OnTextAreaFocus, this));
	},

	_addSelection: function()
	{
		this._addSelectionTops();

		this.Frame = new SSVC.SVG.Group();
		this.Frame.Class = "LabelFrame";
		this.FrameRect = new SSVC.SVG.Rectangle();
		this.FrameRect.Class = "LabelFrame";
		this.FrameTriangle = new SSVC.SVG.Triangle();
		this.FrameTriangle.Class = "LabelFrame";

		this.Frame.addChild(this.FrameTriangle);
		this.Frame.addChild(this.FrameRect);
	},

	_arrangeInnerElements: function()
	{
		this.updateHead();
		this.updateTail(true);
		this._updateText();
	},

	updateHead: function()
	{
		this.Radius = this.OY / 2;

		var lineWidth = (this.InnerElement.LineWidth) ? this.InnerElement.LineWidth : 2;

		var rectCoordinates = this._getRectSpace(this.X, this.Y, this.startX, this.startY);
		this.Frame.update(rectCoordinates.x, rectCoordinates.y, rectCoordinates.ox, rectCoordinates.oy);
		//this.InnerElement.update(this.X, this.Y, this.OX, this.OY);
		this.FrameRect.setRadius(this.Radius);
		this.FrameRect.update(this.X - lineWidth, this.Y - lineWidth, this.OX + lineWidth * 2, this.OY + lineWidth * 2);

		this.InnerRect.setRadius(this.Radius);
		this.InnerRect.update(this.X, this.Y, this.OX, this.OY);
	},

	updateTail: function()
	{
		var lineWidth = (this.InnerElement.LineWidth) ? this.InnerElement.LineWidth : 2;
		this.FrameTriangle.update
			({
				x: this.X + lineWidth * 2 + this.OX / 2 + this.OY / 2 - 3,
				y: this.Y + this.OY / 2
			},
			{
				x: this.startX,
				y: this.startY
			},
			{
				x: this.X - lineWidth * 2 + this.OX / 2 - this.OY / 2 + 3,
				y: this.Y + this.OY / 2
			});


		this.InnerTriangle.update(
			{
				x: this.X + this.OX / 2 + this.OY / 2 - 3,
				y: this.Y + this.OY / 2
			}, {
				x: this.startX,
				y: this.startY
			}, {
				x: this.X + this.OX / 2 - this.OY / 2 + 3,
				y: this.Y + this.OY / 2
			});
	},

	_updateText: function()
	{
		var rect = this.InnerRect.Node.getBoundingClientRect();
		this.TextArea.style.top = +rect.top + "px";
		this.TextArea.style.left = +rect.left + "px";

		this.TextArea.style.color = this.LineColor;
		this.TextArea.style.fontSize = this.FontSize + "pt";
		this.TextArea.style.lineHeight = this.FontSize * 2 + "px";
		this.TextArea.height = this.OY + "px";
		this.TextArea.style.marginLeft = this.Radius + "px";
		this.TextArea.style.fontFamily = this.FontFamily;
	},

	_arrangeGroupNode: function()
	{
		//this.addChild(this.Contour);
		this.addChild(this.Frame);
		this.addChild(this.InnerElement);
		this.Node.appendChild(this.Tops[0].Node);
	},

	_addSelectionTops: function()
	{
		var top = new SSVC.SVG.Circle();
		top.Class = "Top";
		top.Name = "BottomMiddle";
		top.Number = 1;

		SSVC.Wrap.Dojo.On(top.Node, "mousedown", SSVC.Wrap.Dojo.Partial(this.OnTopMouseDown, this, top.Number));
		top.setAttribute("cursor", "default");
		this.Tops.push(top);
	},

	_arrangeTops: function()
	{
		var topRadius = SSVC.SVG.Shape.GetProperty("top");
		var lineWidth = (this.InnerElement.LineWidth) ? this.InnerElement.LineWidth : 2;
		this.Tops[0].update(this.startX, this.startY, topRadius);
	},

	_refreshSelection: function()
	{
		switch (this.SelectMode)
		{
			case this.SelectOptions.Contoured:
				this._showContour();
				this._showInnerElement();
				this._hideSelection();
				break;
			case this.SelectOptions.Selected:
				this._showSelection();
				this._showInnerElement();
				this._hideContour();
				break;
			case this.SelectOptions.None:
				this._hideContour();
				this._hideSelection();
				this._showInnerElement();
				break;
		}
	},

	_showContour: function()
	{
	},

	_hideContour: function()
	{
	},

	_showInnerElement: function()
	{
		this.TextArea.style.display = "block";
	},

	_hideInnerElement: function()
	{
		this.TextArea.style.display = "none";
	},

	ScaleX: function(factor)
	{
		if (factor <= 0)
			return;

		this.X *= factor;
		this.startX *= factor;
		this.Refresh();
	},

	ScaleY: function(factor)
	{
		if (factor <= 0)
			return;

		this.Y *= factor;
		this.startY *= factor;
		this.Refresh();
	},

	GetOuterXml: function()
	{
		var properties = [];

		var resultXml = this.getXml();
		var tailXml = this.InnerTriangle.getXml();
		var rectXml = this.InnerRect.getXml();
		var textNode = new SSVC.SVG.Text();

		for (var i = 0; i < this.TextArea.attributes.length; i++)
		{
			textNode.Node.setAttribute(this.TextArea.attributes[i].name, this.TextArea.attributes[i].value);
		}

		textNode.Node.textContent = this.TextArea.textContent;

		var textXml = textNode.getXml();

		resultXml.appendChild(textXml);
		resultXml.appendChild(tailXml);
		resultXml.appendChild(rectXml);

		resultXml.setAttribute("complextype", "label");

		return resultXml;
	},

	SetXml: function(xmldoc)
	{
		var innerRect = xmldoc.getElementsByTagName("rect")[0];
		var innerTriangle = xmldoc.getElementsByTagName("polygon")[0];
		var textArea = xmldoc.getElementsByTagName("text")[0];

		for (var i = 0; i < textArea.attributes.length; i++)
		{
			this.TextArea.setAttribute(textArea.attributes[i].name, textArea.attributes[i].value);
		}

		this.InnerRect.applyXml(innerRect);
		this.InnerTriangle.applyXml(innerTriangle);

		this.applyXml(xmldoc);
				
		this.TextArea.textContent = textArea.textContent;

		if (this.InnerTriangle.pointList.length == 0)
			this.InnerTriangle.getPoints();

		this.startX = this.InnerTriangle.pointList[1][0];
		this.startY = this.InnerTriangle.pointList[1][1];

		this.Refresh();
	},

	Fill: function(rgb)
	{
		this.TextArea.style.background = rgb;
	},

	Delete: function()
	{
		this.Node.parentNode.removeChild(this.Node);
		this.TextArea.parentNode.removeChild(this.TextArea);
	}
}));
